package cup.com.icss.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cup.com.icss.dao.DormitoryMapper;
import cup.com.icss.dao.TheRepairMapper;
import cup.com.icss.model.Dormitory;
import cup.com.icss.model.TheRepair;
import cup.com.icss.model.User;
import cup.com.icss.model.WaterElectricity;
import cup.com.icss.utils.LogUtil;
import cup.com.icss.utils.StringUtil;

@Controller
public class theRepairController {
	@Resource
	private TheRepairMapper theRepairMapper;
	@Resource
	private DormitoryMapper dormitoryMapper;
	
	@RequestMapping("/theRepairInitialize")
	@ResponseBody
	public ModelAndView theRepairInitialize(HttpServletRequest request){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			view.addObject("storiedBuilding",user.getStoriedBuilding());
			view.addObject("roleName",user.getRoleName());
			view.addObject("studentId",user.getStudentId());
			view.setViewName("cup/jsp/theRepair/theRepairInitialize");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/theRepairList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> theRepairList(HttpServletRequest request, Integer rows,Integer page, TheRepair record,String roleName, String studentId){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			
			page=page==null?1:page;
			rows=rows==null?10:rows;
			int pageNum = page;
			int pageSize = rows;
			if(roleName.equals("ADMINISTRATOR")){
				int num = theRepairMapper.selectTheRepairListSize(record).size();
				int pageSizez;
				if(((pageSize*pageNum)-num)<0){
					pageSizez = pageSize;
				}else{
					pageSizez = num-(10*(pageNum-1));
				}
				List<TheRepair> somethingLost = theRepairMapper.selectTheRepairList(pageNum*pageSize-pageSize,pageSizez,record);
				map.put("total", num);
				map.put("rows", somethingLost);
			}else{
				Dormitory dormitory = dormitoryMapper.selectStudentIdObj(studentId);
				record.setDormitory(dormitory.getDormitory());
				int num = theRepairMapper.selectTheRepairListSize(record).size();
				int pageSizez;
				if(((pageSize*pageNum)-num)<0){
					pageSizez = pageSize;
				}else{
					pageSizez = num-(10*(pageNum-1));
				}
				List<TheRepair> somethingLost = theRepairMapper.selectTheRepairList(pageNum*pageSize-pageSize,pageSizez,record);
				map.put("total", num);
				map.put("rows", somethingLost);
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping("/theRepairAdd")
	@ResponseBody
	public ModelAndView theRepairAdd(HttpServletRequest request){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			view.addObject("storiedBuilding",user.getStoriedBuilding());
			view.addObject("user",user);
			view.addObject("roleName",user.getRoleName());
			view.addObject("studentId",user.getStudentId());
			Dormitory dormitory = dormitoryMapper.selectStudentIdObj(user.getStudentId());
			if(dormitory!=null && dormitory.getDormitory()!=null){
				view.addObject("dormitory",dormitory.getDormitory());
				view.setViewName("cup/jsp/theRepair/theRepairAdd");
			}else{
				view.setViewName("cup/jsp/theRepair/theRepairError");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/theRepairAddData",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> theRepairAddData(HttpServletRequest request,TheRepair record){
		Map<String, String> map = new HashMap<String,String>();
		try {
			Date time = new Date();
			record.setTime(time);
			record.setStatus("1");
			record.setId(StringUtil.getUUID());
			int inster = theRepairMapper.insertSelective(record);
			if(inster>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/theRepairEdit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> theRepairEdit(HttpServletRequest request, TheRepair record){
		Map<String, String> map = new HashMap<String,String>();
		try {
			record.setStatus("0");
			int update = theRepairMapper.updateByPrimaryKeySelective(record);
			if(update>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/theRepairDel",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> theRepairDel(HttpServletRequest request, String id){
		Map<String, String> map = new HashMap<String,String>();
		try {
			
			int del = theRepairMapper.deleteByPrimaryKey(id);
			if(del>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
}
